package model

import (
	"fmt"
	"time"
)

// UserCreateRequest represents the user creation request structure
type UserCreateRequest struct {
	Name     string `json:"name" binding:"required"`
	Email    string `json:"email" binding:"required,email"`
	Password string `json:"password" binding:"required"`
}

// UserResponse represents the user response structure
type UserResponseData struct {
	ID          string     `json:"id"`
	Email       string     `json:"email"`
	Name        string     `json:"name"`
	FirstName   *string    `json:"first_name,omitempty"`
	LastName    *string    `json:"last_name,omitempty"`
	VerifiedAt  *time.Time `json:"verified_at,omitempty"`
	Image       *string    `json:"image,omitempty"`
	Address     *string    `json:"address,omitempty"`
	Region      *string    `json:"region,omitempty"`
	Mobile      *string    `json:"mobile,omitempty"`
	Role        int16      `json:"role"`
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   *time.Time `json:"updated_at,omitempty"`
	DeletedAt   *time.Time `json:"deleted_at,omitempty"`
	HasOAuth    bool       `json:"has_oauth"`    // 新增字段：是否有OAuth关联
	HasPassword bool       `json:"has_password"` // 新增字段：是否设置了密码
}

type UserResponse struct {
	Response
	Data UserResponseData `json:"data"`
}

// UserUpdateRequest represents the user update request structure
type UserUpdateRequest struct {
	Name      *string `json:"name,omitempty"`
	FirstName *string `json:"first_name,omitempty"`
	LastName  *string `json:"last_name,omitempty"`
	Image     *string `json:"image,omitempty"`
	Address   *string `json:"address,omitempty"`
	Region    *string `json:"region,omitempty"`
	Mobile    *string `json:"mobile,omitempty"`
}

// UserPasswordUpdateRequest represents the user password update request structure
type UserPasswordUpdateRequest struct {
	CurrentPassword string `json:"current_password" binding:"required"`
	NewPassword     string `json:"new_password" binding:"required,min=6"`
	ConfirmPassword string `json:"confirm_password" binding:"required,min=6"`
}

// Validate validates the UserPasswordUpdateRequest
func (r *UserPasswordUpdateRequest) Validate() error {
	if r.NewPassword != r.ConfirmPassword {
		return fmt.Errorf("new password and confirm password do not match")
	}
	return nil
}

// UserPasswordSetRequest represents the user password set request structure (for OAuth users)
type UserPasswordSetRequest struct {
	NewPassword     string `json:"new_password" binding:"required,min=6"`
	ConfirmPassword string `json:"confirm_password" binding:"required,min=6"`
}

// Validate validates the UserPasswordSetRequest
func (r *UserPasswordSetRequest) Validate() error {
	if r.NewPassword != r.ConfirmPassword {
		return fmt.Errorf("new password and confirm password do not match")
	}
	return nil
}

// UserEmailCaptchaRequest represents the email captcha request structure
type UserEmailCaptchaRequest struct {
	Email string `json:"email" binding:"required,email"`
}

// UserEmailUpdateRequest represents the email update request structure
type UserEmailUpdateRequest struct {
	Email   string `json:"email" binding:"required,email"`
	Captcha string `json:"captcha" binding:"required"`
}

// UserMobileCaptchaRequest represents the mobile captcha request structure
type UserMobileCaptchaRequest struct {
	Mobile string `json:"mobile" binding:"required"`
}

// UserMobileUpdateRequest represents the mobile update request structure
type UserMobileUpdateRequest struct {
	Mobile  string `json:"mobile" binding:"required"`
	Captcha string `json:"captcha" binding:"required"`
}

// UserDeleteRequest represents the user delete request structure
type UserDeleteRequest struct {
	Password *string `json:"password,omitempty"` // 可选字段，当用户有密码时必须提供
}
